//
//  XXLocalStore.swift
//  LEDSMART
//
//  Created by LXF on 2022/9/29.
//  Copyright © 2022 LXF. All rights reserved.
//

import UIKit

fileprivate let ___KEY = "LocalStore";

public protocol KeyProtocol {
    func xx_makeKey(para:Any?)->String
    func xx_toString()->String
}

public extension KeyProtocol{
    
    func xx_makeKey(para: Any?) -> String {
        return "\(___KEY)_\(self.xx_toString())_\(para ?? "-")"
    }
    
    func xx_toString()->String{
        return "\(self)"
    }
    
    
    
}


public class LocalStore{
    
    public static func save(key:KeyProtocol,para:Any? = "-",content:String){
        let keyString = key.xx_makeKey(para:para)
        UserDefaults.standard.set(content, forKey:keyString)
    }
    
    public static func load(key:KeyProtocol,para:Any? = "-",defaultValue:String? = nil)->String?{
        let keyString = key.xx_makeKey(para:para)
        return UserDefaults.standard.value(forKey: keyString) as? String ?? defaultValue
    }
    
    public static func save(key:KeyProtocol,para:Any? = "-",content:Int){
        let keyString = key.xx_makeKey(para:para)
        UserDefaults.standard.set(content, forKey:keyString)
    }
    
    public static func loadToInt(key:KeyProtocol,para:Any? = "-",defaultValue:Int)->Int{
        let keyString = key.xx_makeKey(para:para)
        return UserDefaults.standard.value(forKey: keyString) as? Int ?? defaultValue
    }
    
}
